package zcj.stu;

import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
import org.springframework.web.servlet.resource.CachingResourceResolver;
import org.springframework.web.servlet.resource.EncodedResourceResolver;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import java.util.List;
import java.util.Locale;
import java.util.Map;

@Configuration
public class WebConfig {
    //利用内嵌web容器工厂创建web容器
    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(){
        return new TomcatServletWebServerFactory(8888);
    }
    //创建dispatchServlet
    @Bean
    public DispatcherServlet dispatcherServlet(){
        return new DispatcherServlet();
    }
    //将dispatchServlet注册到web容器中
    @Bean
    public DispatcherServletRegistrationBean registrationBean(DispatcherServlet dispatcherServlet){
        DispatcherServletRegistrationBean registrationBean =
                new DispatcherServletRegistrationBean(dispatcherServlet, "/");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;//所有路径
    }
    @Bean
    public SimpleUrlHandlerMapping mapping(ApplicationContext context){
        Map<String, ResourceHttpRequestHandler> beans = context.getBeansOfType(ResourceHttpRequestHandler.class);
        System.out.println(beans);
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setUrlMap(beans);
        return mapping;
    }
    @Bean
    public HttpRequestHandlerAdapter adapter(){
        return new HttpRequestHandlerAdapter();
    }
    @Bean("/**")
    public ResourceHttpRequestHandler handler1(){
        ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
        handler.setLocations(List.of(new ClassPathResource("static/")));
        ConcurrentMapCache staticCache = new ConcurrentMapCache("staticCache");
        System.out.println(staticCache);
        handler.setResourceResolvers(List.of(
                new CachingResourceResolver(staticCache),
                new EncodedResourceResolver(),
                new PathResourceResolver()
        ));
        return handler;
    }
    @Bean("/img/**")
    public ResourceHttpRequestHandler handler2(){
        ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
        handler.setLocations(List.of(new ClassPathResource("images/")));
        return handler;
    }
    @Bean("/")
    public ResourceHttpRequestHandler handler3(){
        ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
        handler.setLocations(List.of(new ClassPathResource("/index.html")));
        return handler;
    }
}
